package com.ruoyi.hr.service.impl.welfare.welfarecalculation;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.welfarecalculationconfig.CcdBenefititem;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.welfare.welfarebaseadjustment.SocialSecurityBaseAdjustmentMapper;
import com.ruoyi.hr.mapper.welfare.welfarecalculation.WelfareCalculationMapper;
import com.ruoyi.hr.mapper.welfare.welfarecalculationconfig.CcdBenefititemMapper;
import com.ruoyi.hr.service.welfare.welfarecalculation.WelfareCalculationService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WelfareCalculationServiceImpl implements WelfareCalculationService {

    @Autowired
    SocialSecurityBaseAdjustmentMapper socialSecurityBaseAdjustmentMapper;

    @Autowired
    WelfareCalculationMapper welfareCalculationMapper;

    @Autowired
    CcdBenefititemMapper ccdBenefititemMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findEmpWelfareData(String pgid, Map map1, List<Authority> authorities, int isAdmin) {
        String name = map1.get("name").toString();
        String badge = map1.get("badge").toString();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pgid", pgid);
        //拼接动态SQL
        List<CcdBenefititem> ccdBenefititems = ccdBenefititemMapper.findAlls();
        map.put("pro", ccdBenefititems);
        map.put("name", name);
        map.put("badge", badge);
        List<Map<String, Object>> list = welfareCalculationMapper.findEmpWelfareData(map,authorities,isAdmin);
        return list;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findWelfareProcess() {
        return welfareCalculationMapper.findWelfareProcess();
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult executeProcedure(String allid, String procedure) {
        //叠加异常信息
        String msgAdd = "";
        try {
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] ids = allid.split(",");
            String msg;//异常信息
            for (String id : ids) {
                sql = "declare @RetVal varchar(50) exec " + procedure + " " + id + "," + maps.get("id").toString() + ",100" + ",@RetVal output select @RetVal";
                List<Map<String,Object>> maps1 = welfareCalculationMapper.executeProcedure(sql);
                if(maps1.size() == 1){
                    //1代表有返回消息
                    msg = skyMsgAlarmMapper.getMsgById(maps1.get(0).get("").toString());
                    if (msg != null) {
                        msgAdd += welfareCalculationMapper.findxztName(id) + ":" + msg + "\n";
                    }
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }

        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }
}
